use common::model::ProjectDependency;
use common::util::run_command;
use std::path::Path;

pub fn run_install(
    dependency: &ProjectDependency,
    project_dir: &Path,
) -> Result<(), Box<dyn std::error::Error>> {
    install_npm_module(dependency, project_dir)?;
    Ok(())
}

fn install_npm_module(
    dependency: &ProjectDependency,
    project_dir: &Path,
) -> Result<(), Box<dyn std::error::Error>> {
    println!("开始安装:{}", dependency.url);
    Ok(run_command(
        project_dir,
        "npm",
        &[
            "install",
            if dependency.is_dev {
                "--save-dev"
            } else {
                "--save"
            },
            &dependency.url,
        ],
    )?)
}
